<template>
	
	<view >
		<view  v-if="showhide ==1">
			<view class="questions-nav">
					<view class="questions-nav-title">难度：{{info.level.name}}</view>
					<view class="questions-nav-right">{{ formatTime(timer) }}</view>
					<button class="sharebutton questions-nav-explain share" open-type="share">
						<image src="../../static/questions/share.png"></image>
						<view>一键分享</view>
					</button>
					<view class="questions-nav-explain" @click="explainChange(1)">
						<image src="../../static/questions/shuo.png"></image>
						<view>说明</view>
					</view>
				</view>
				
				<All :info="info"   :bijishow="bijishow" @userinputChange="userinputChange"></All>
				
				<Function
					:tskeys = "tskeys"
					:index = "index"  
					:index1 ="index1"
					:tshintnumber="tshintnumber"
					@bijiChange = "bijiChange"
					@hintChange="hintChange"
					@erasureChange="erasureChange"
					@revocationChange="revocationChange"
					@submitChange  = "submitChange"
					@advertisingChange = "advertisingChange" 
				></Function> 
				<Biji v-if="bijishow == 1" 
					@bijiNumChange="bijiNumChange" 
					:info="info"
					:index="index"
					:index1="index1"
					:bijireplace="bijireplace"
				></Biji>
				<Imports v-if="bijishow == 0"  @numberChange="numberChange" :info="info"></Imports>
				<Popup v-if="is_show==1" :timer="timer" :info="info" :questions_user_id="questions_user_id" :is_date="is_date"></Popup>
				<Explain :info="info" v-if="is_explain == 1" @explainChange="explainChange"></Explain> 
				
				<ad unit-id="adunit-e216b11c44862965" bindload="adLoad" binderror="adError" bindclose="adClose"></ad>
			</view>
		</view>  
		
</template>

<script>
	import All  from "../../components/cell/all.vue"
	import  Biji  from "../../components/cell/biji.vue"
	
	import Function from  "../../components/cell/function.vue"
	import Imports from  "../../components/cell/import.vue"
	import Popup from  "../../components/cell/popup.vue"
	import Explain from "../../components/cell/explain.vue"
	
	
	import {Calculate,numSub,cachequestions} from '../../common/uits/questions.js'
	export default {
		components:{
			Biji,All,
			Function,Imports,Popup,Explain
		},
		data() {
			return {
				timer: 0,
				id : '',
				bid : '',
				lid : '',
				info : {},
				number : '',
				index  : '',
				index1 : '',
				sequence: [], //答题顺序
				schedule: [], //答题进度
				Intervaltime : '',
				questions_user_id : '',
				is_show  :   0,
				is_explain :0,
				is_date  : 1,
				showhide : 0,
				hintnumber: 0,  //提示次数  传入后端
				tshintnumber: 0,  //提示次数 前端使用
				query    : {}, //参数
				tskeys     : '',  
				bijishow : 0 , //笔记   
				bijireplace : 0,  // 笔记  更换笔记的时候 +1；
				user_id    : uni.getStorageSync("user_id"),
				rewardedVideoAd : '',
				
			}
		},
		mounted() {
			this.Intervaltime = setInterval(() => {
		      this.timer++;
		    }, 1000);
			
		},
		onLoad(e) {
			this.id  = e.id;
			this.lid = e.lid;
			this.bid = e.bid;
			this.query =  e;
			this.tskeys  = this.user_id+'q'+this.id;
			if(e.hasOwnProperty('is_date')){
				this.is_date = e.is_date;
			}
			this.keys  = 'questions'+uni.getStorageSync('user_id');
			var  value  = uni.getStorageSync(this.keys);
			
			uni.showToast({
				title: '加载中',
				icon: 'loading'
			},4000);
			if(value){
				value   = JSON.parse(value);
				console.log(value);
				if(value.id  ==  this.id){
					this.info  = value.questions;
					this.showhide  = 1;
					this.timer = value.timer;
					uni.hideToast();
				}else{
					this.getinfo();
				}
			}else{
				this.getinfo();
			}
			this.gettshintnumber();
			if(wx.createRewardedVideoAd){
				this.rewardedVideoAd = wx.createRewardedVideoAd({ adUnitId: 'adunit-7b1fc9458a766b9f' })
				this.rewardedVideoAd.onLoad(() => {
					console.log('onLoad event emit')
				})
				this.rewardedVideoAd.onError((err) => {
					console.log('onError event emit', err)
				})
				this.rewardedVideoAd.onClose((res) => {
					 console.log(res);
					 if (res && res.isEnded) {
						 console.log(222222222222);
						uni.setStorageSync(this.tskeys,5);
						
						this.tshintnumber  = 5;
						console.log(this.tshintnumber)
					}
					console.log('onClose event emit', res)
				})
			}
			
		},
		methods: {
			bijiChange(e){
				this.bijishow =  e;
			},
			explainChange(e){
				this.is_explain  = e;
			},
			numberChange(e){
				this.number  = e;
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][this.index][this.index1]['uservalue']  = this.number;
				}else{
					this.info.body[this.index][this.index1]['uservalue']  = this.number;
				}
				this.sequence =  Calculate(this.sequence,this.index,this.index1,0,this.number);
				this.schedule =  Calculate(this.schedule,this.index,this.index1,1,this.number);
				cachequestions(this.query,this.info,this.info.id,this.timer,this.keys);
				
			},
			bijiNumChange(e){
				console.log(new Date().toLocaleString());
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					var  biji  = this.info.body['rule'][this.index][this.index1]['biji'];
					if(biji.indexOf(e) == -1){
						biji.push(e);
					}else{
						biji.splice(biji.indexOf(e),1);
					}
					this.info.body['rule'][this.index][this.index1]['biji']  =biji;
				}else{
					var  biji  = this.info.body[this.index][this.index1]['biji'];
					if(biji.indexOf(e) == -1){
						biji.push(e);
					}else{
						biji.splice(biji.indexOf(e),1);
					}
					this.info.body[this.index][this.index1]['biji']  = biji;
					
				}
				console.log(new Date().toLocaleString());
			},
			userinputChange(e){
				this.index  = e.index;
				this.index1  = e.index1;
			
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][e.index][e.index1]['ticolor']  = '';
				}else{
					this.info.body[e.index][e.index1]['ticolor']  = '';
				}
				cachequestions(this.query,this.info,this.info.id,this.timer,this.keys);
				if(this.bijishow == 1) {
					this.bijireplace++;
				}
				
			},
			
			hintChange(e){   //提示
				this.hintnumber++;
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][this.index][this.index1]['uservalue']  = this.info.body['rule'][this.index][this.index1]['value'];
					this.schedule =  Calculate(this.schedule,this.index,this.index1,1,this.info.body['rule'][this.index][this.index1]['value']);
				}else{
					this.info.body[this.index][this.index1]['uservalue']  = this.info.body[this.index][this.index1]['value'];
					this.schedule =  Calculate(this.schedule,this.index,this.index1,1,this.info.body[this.index][this.index1]['value']);
				}
				this.sequence = Calculate(this.sequence,this.index,this.index1);
				cachequestions(this.query,this.info,this.info.id,this.timer,this.keys);
				
			},
			erasureChange(e){  //擦除
				if(this.info['cid'] == 12 || this.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					this.info.body['rule'][this.index][this.index1]['uservalue']  =  '';
					this.schedule =  Calculate(this.schedule,this.index,this.index1,1,this.info.body['rule'][this.index][this.index1]['value']);
				}else{
					this.info.body[this.index][this.index1]['uservalue']  =  '';
					this.schedule =  Calculate(this.schedule,this.index,this.index1,1,this.info.body[this.index][this.index1]['value']);
				}
				cachequestions(this.query,this.info,this.info.id,this.timer,this.keys);
			},
			
			submitChange(e){  //提交
				let  that = this;
				var  body = that.info.body;
				if(that.info['cid'] == 12 || that.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
					body  = that.info.body['rule'];
				}
				var  is_type  = 0;
				for(var i = 0 ; i < body.length; i++){
					for(var j = 0; j < body[i].length;j++){
						if(body[i][j]['type']  == 1 && (body[i][j]['value']  != body[i][j]['uservalue'])){
							is_type  = 1;
						}
					}
				}
				if(is_type == 1){
					
					uni.showModal({
						title: '提示',
						content: '请查看正确答案！',
						success: function (res) {
							if (res.confirm) {
								for(var i = 0 ; i < body.length; i++){
									for(var j = 0; j < body[i].length;j++){
										if(body[i][j]['type']  == 1 ){
											if(body[i][j]['uservalue']  != body[i][j]['value']){
												body[i][j]['ticolor']  =  'red';
											}
										}
									}
								}
									if(that.info['cid'] == 12 || that.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
									that.info.body['rule'] =  body;
								}else{
									that.info.body =  body;
								}
								return;
							}
						},
					})
					
					return;
				}
				var parmes  = {
					'cid'   : that.info.cid,
					'qid'   : that.info.id,
					'bid'   : that.info.bid,
					'body'   : JSON.stringify(that.info.body),
					'schedule': JSON.stringify(that.schedule),
					'timer'   :that.timer,
					'is_date':that.is_date,
					'hintnumber':that.hintnumber
				}
				
				that.$questions.submit(parmes).then(res=>{
					uni.showToast({
						title: res.msg,
						icon: "none" ,
					});
					if(res.code == 1){
						that.questions_user_id  = res.data.id;
						that.is_show  = 1;
						
						uni.setStorageSync(that.keys,'');
						uni.setStorageSync(that.tskeys,'');
						
						clearInterval(that.Intervaltime)
					}
				})
				
				console.log(parmes);
			},
			revocationChange(e){
				let that  = this;
				var length =  that.sequence.length;
				length  = numSub(length,1);
				if(length >=0){
					if(that.info['cid'] == 12 || that.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
						that.info.body['rule'][that.sequence[length]['index']][that.sequence[length]['index1']]['uservalue']  = '';
					}else{
						that.info.body[that.sequence[length]['index']][that.sequence[length]['index1']]['uservalue']  = '';
					}
					that.sequence.splice(length,1);
				}
				cachequestions(this.query,this.info,this.info.id,this.timer,this.keys);
			},
			getinfo(){
				let  that = this;
				
				that.$questions.getdetails({'ids':that.id,'is_date':that.is_date,'lid':that.lid}).then(res=>{
					that.info  = res.data.info;
					if(that.info['cid'] == 12 || that.info['cid'] == 13 || this.info['cid'] == 15 || this.info['cid'] == 19){
						var  body  = that.info.body;
						for(var i = 0 ; i < body['rule'].length; i++){
							for(var j = 0; j < body['rule'][i].length;j++){
								body['rule'][i][j]['biji']   = [];
								if(body['rule'][i][j]['type']  == 1 ){
									body['rule'][i][j]['ticolor']  =  '';
								}
							}
						}
						
					}else{
						var  body  = that.info.body;
						for(var i = 0 ; i < body.length; i++){
							for(var j = 0; j < body[i].length;j++){
								body[i][j]['biji']   = [];
								if(body[i][j]['type']  == 1 ){
									body[i][j]['ticolor']  =  '';
									
								}
							}
						}
					}
					that.info.body = body;
					that.showhide  = 1;
					uni.hideToast();
					uni.setNavigationBarTitle({
					    title: that.info.name
					})
				})
				
				
			},
			gettshintnumber(){
				let that = this;
				if(!uni.getStorageSync(that.tskeys)){
					that.$api.getConfig({'keys':'hintnumber'}).then(res=>{
						that.tshintnumber  = res.data.value;
						uni.setStorageSync(that.tskeys,that.hintnumber);
					})
				}else{
					console.log('==========');
					that.tshintnumber  = uni.getStorageSync(that.tskeys);
					console.log(that.tshintnumber);
				}
			},
			formatTime(time) {
			      // 将时间格式化为 hh:mm:ss 的形式
			      let hour = Math.floor(time / 3600);
			      let minute = Math.floor((time - hour * 3600) / 60);
			      let second = Math.floor(time - hour * 3600 - minute * 60);
			      return `${hour.toString().padStart(2, '0')}:${minute
			        .toString()
			        .padStart(2, '0')}:${second.toString().padStart(2, '0')}`;
			},
			advertisingChange(e){  //广告
				this.tshintnumber =  0;
				this.rewardedVideoAd.show();
			},
		},
		onShareAppMessage() {
			
		     return {
		           title: '您的朋友向您推荐数独星球中的试题【'+this.info.name+'】',
		          imgUrl: ''     //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支 
		                            // 持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
		      }
		},
		onShow() {
			let that  =this;
			wx.login({
			    success(res) {
					if (res.code) {
						that.$user.login({'code':res.code}).then(res=>{
							if(res.code == 1){
								uni.setStorageSync("user_id",res.data.user_id);
							}
						})
			        } else {
						console.log('登录失败！' + res.errMsg)
					}
			    }
			});
		},
		adLoad() {
		    console.log('Banner 广告加载成功')
		},
		adError(err) {
		    console.error('Banner 广告加载失败', err)
		},
		adClose() {
		    console.log('Banner 广告关闭')
		}
	}
</script>

<style>

	
	
</style>
